package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ShopCar;
import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.mapper.ShopCarMapper;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.service.IShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopCarServiceImpl implements IShopCarService {

    @Autowired
    private ShopCarMapper shopCarMapper;

    @Autowired
    private SignMapper signMapper;

    //加入购物车
    @Override
    public void insert(ShopCar record) {
        ShopCar shopCar = shopCarMapper.selectByGoodsId(record.getGoods().getId(), record.getUserId());
        if (shopCar == null) {
            shopCarMapper.insert(record);
        } else {
            shopCarMapper.update(record);
        }
    }




    //回显购物车数据
    @Override
    public List<ShopCar> selectByUserId(Long userId) {
        List<ShopCar> shopCars = shopCarMapper.selectByUserId(userId);
        System.out.println(shopCars);
        return shopCars;
    }




    //结算是否成功
    @Override
    public Boolean Calculation(Long userId, int sum) {
        Sign sign = signMapper.selectIntByuserId(userId);
        int Integral = sign.getIntegral();
        if (Integral - sum > 0) {
            int cc = Integral-sum;
            signMapper.updateIntegral(userId,cc);
            return true;
        } else {

            return false;
        }
    }

}
